@page "/Admin/User"

<PageTitle>用户管理</PageTitle>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">

            <AdminTable2 TItem="UserEntity" Context="item" PageSize="20" TableTd99Width="230"
                
                OnQuery="OnQuery" InitQuery="InitQuery" OnEdit="OnEdit" OnRemove="OnRemove">

                <CardHeader>
                    <button @onclick="ShowUserLogin" type="button" class="mr-2 btn btn-light btn-sm"><i class="fas fa-camera"></i> 登陆日志</button>
                </CardHeader>

                <TableHeader>
                    <th>名称</th>
                    <th>昵称</th>
                    <th>登陆时间</th>
                    <th>创建时间</th>
                    <th>角色</th>
                </TableHeader>
                <TableRow>
                    <td>@item.Username</td>
                    <td>@item.Nickname</td>
                    <td>@item.LoginTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                    <td>@item.CreatedTime?.ToString("yyyy-MM-dd HH:mm:ss")</td>
                    <td>@string.Join(", ", item.Roles.Select(a => a.Name))</td>
                </TableRow>
                <TableTd99>
                    <button @onclick="e => BeginAllocRoles(item)" type="button" class="mr-2 btn btn-light btn-xs"><i class="fa fa-user-secret"></i>分配角色</button>
                </TableTd99>
                <EditTemplate>
                    <div class="row">
                        <div class="form-group col-6">
                            <label class="form-label">名称</label>
                            <input @bind="item.Username" type="text" class="form-control" placeholder="" maxlength="50">
                        </div>
                        <div class="form-group col-6">
                            <label class="form-label">昵称</label>
                            <input @bind="item.Nickname" type="text" class="form-control" placeholder="" maxlength="50">
                        </div>
                        <div class="form-group col-6">
                            <label class="form-label">密码</label>
                            <input @bind="item.Password" type="text" class="form-control" placeholder="" maxlength="50">
                        </div>
                    </div>
                </EditTemplate>
            </AdminTable2>
        </div>
    </div>
</div>

<AllocTable2 TItem="UserEntity" TChild="RoleEntity" @bind-Item="@allocItemRoles" ChildProperty="Roles" Title="@($"【分配角色】{allocItemRoles?.Username}")">
    <TableTd1>@context.Name</TableTd1>
</AllocTable2>

<AdminModal Visible="showUserLogin" Title="登陆日志" OnClose="e => showUserLogin = false" DialogClassName="modal-xxl">
    <AdminTable2 TItem="UserLoginEntity" Context="item" PageSize="20"
        IsAdd="false" IsEdit="false" IsRemove="false" IsSingleSelect="false" IsMultiSelect="false"
        OnQuery="OnQueryUserLogin" InitQuery="InitQueryUserLogin">

        <TableHeader>
            <th>登陆时间</th>
            <th>用户名</th>
            <th>日志类型</th>
            <th>IP</th>
            <th>地点</th>
            <th>操作系统</th>
            <th>设备类型</th>
            <th>浏览器</th>
            <th>浏览器语言</th>
        </TableHeader>
        <TableRow>
            <td>@item.LoginTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
            <td>@item.Username</td>
            <td>@item.Type</td>
            <td>@item.Ip</td>
            <td>@item.City</td>
            <td>@item.OS</td>
            <td>@item.Device</td>
            <td>@item.Browser</td>
            <td>@item.Language</td>
        </TableRow>
    </AdminTable2>
</AdminModal>

@code {

    [Inject] IAggregateRootRepository<UserEntity> repo { get; set; }
    [Inject] AdminContext admin { get; set; }

    async Task InitQuery(AdminQueryInfo e)
    {
        var allRoleEntities = await repo.Orm.Select<RoleEntity>().ToListAsync();
        e.Filters = new AdminFilterInfo[]
        {
            new AdminFilterInfo("角色", "Roles", true, 12, 
                string.Join(",", allRoleEntities.Select(a => a.Name)),
                string.Join(",", allRoleEntities.Select(a => a.Id))),
        };
        await Task.Yield();
    }
    void OnQuery(AdminQueryEventArgs<UserEntity> e)
    {
        e.Select.IncludeMany(a => a.Roles)
            .WhereIf(e.Filters[0].HasValue, a => a.Roles.Any(b => e.Filters[0].Values<long>().Contains(b.Id)))
            .WhereIf(!e.SearchText.IsNull(), a => a.Username.Contains(e.SearchText) || a.Nickname.Contains(e.SearchText));
    }
    async Task OnRemove(AdminRemoveEventArgs<UserEntity> e)
    {
        if (e.Items.Any(a => a.Id == admin.User.Id))
        {
            await JS.Error("不能删除当前登陆的账号!");
            e.Cancel = true;
        }
    }

    async Task OnEdit(UserEntity item)
    {
        await Task.Yield();
    }
    UserEntity allocItemRoles;
    [AdminButton("alloc_roles")]
    async Task BeginAllocRoles(UserEntity item)
    {
        allocItemRoles = item;
        await Task.Yield();
    }


    bool showUserLogin;
    [AdminButton("登陆日志")]
    void ShowUserLogin()
    {
        showUserLogin = true;
    }
    async Task InitQueryUserLogin(AdminQueryInfo e)
    {
        e.Filters = new AdminFilterInfo[]
        {
            new AdminFilterInfo("日志", "Type", "登陆成功,登陆失败", "0,1"),
            new AdminFilterInfo("设备", "Device", "PC,Mobile,Tablet", "0,1,2"),
        };
        await Task.Yield();
    }
    void OnQueryUserLogin(AdminQueryEventArgs<UserLoginEntity> e)
    {
        e.Select.WhereIf(e.Filters[0].HasValue, a => a.Type == e.Filters[0].Value<UserLoginEntity.LoginType>())
            .WhereIf(e.Filters[1].HasValue, a => a.Device == e.Filters[1].Value<WebClientDeviceType>())
            .WhereIf(!e.SearchText.IsNull(), a => a.Ip.Contains(e.SearchText) || a.Username.Contains(e.SearchText))
            .OrderByDescending(a => a.Id);
    }
}
